//
//  TravelViewController.m
//  MiaoTu
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelViewController.h"
#import "UIView+Frame.h"
#import "Masonry.h"
#import "ZWYNetTool.h"
#import "TravelModel.h"
#import "MTMacro.h"
#import "HotPlaceCollectionCell.h"
#import "SDCycleScrollView.h"
#import "Banner.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "TravelItem.h"
#import "TravelTableCell.h"
#import "PersonalHomeViewController.h"
#import "LineDetailViewController.h"
#import "SearchViewController.h"
#import "TravelGoTableCell.h"
#import "PhotoAlbumViewController.h"
#import "YueyouViewController.h"
#import "LoginViewController.h"
#import "ReplyViewController.h"

#define kHotPlaceCollectionCell @"HotPlaceCollectionCellIdentifier"
#define kMoreHotPlaceCollectionCell @"MoreHotPlaceCollectionCellIdentifier"
#define kTravelTableCell @"TravelTableCellIdentifier"
#define kTravelGoTableCell @"TravelGoTableCellIdentifier"

#define kOrginateButtonWidth 46

const NSInteger kHotCollectionHeight = 45;

typedef NS_ENUM(NSUInteger, SortType) {
    SortTypeDefault,
    SortTypeOnlyBoy,
    SortTypeOnlyGirl,
    SortTypeByDistance,
    SortTypeByPopularity
};

@interface TravelViewController ()
<
    UICollectionViewDataSource,
    UICollectionViewDelegate,
    UICollectionViewDelegateFlowLayout,
    SDCycleScrollViewDelegate,
    UITableViewDataSource,
    UITableViewDelegate,
    TravelTableCellDelegate
>
{
    BOOL flag;
    BOOL isFistLoad;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TravelModel *travelModel;
@property (nonatomic, strong) UICollectionView *hotPlaceCollection;
@property (nonatomic, strong) SDCycleScrollView *bannerCycleView;
@property (nonatomic, strong) UILabel *hotPlaceLabel;
@property (nonatomic, strong) UIView *moreHotPlaceBackView;
@property (nonatomic, strong) UICollectionView *moreHotPlaceCollection;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray<TravelItem *> *travelItemArray;
@property (nonatomic, strong) UIImageView *whiteTriangleImage;
@property (nonatomic, strong) UIView *sortView;
@property (nonatomic, strong) UIView *stopView;
@property (nonatomic, assign) SortType sortType;
@property (nonatomic, strong) UITapGestureRecognizer *hideSortTap;
@property (nonatomic, strong) UIButton *sortButton;
@property (nonatomic, strong) NSArray<Topic *> *topicArray;
@property (nonatomic, strong) UIButton *yueyouButton;
@property (nonatomic, assign) BOOL popYueyouFlag;
@property (nonatomic, strong) UILabel *likeAlertLabel;
@end

@implementation TravelViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setUpNavigationTabBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    flag = YES;
    isFistLoad = YES;
    self.popYueyouFlag = NO;
    
    self.sortType = SortTypeDefault;
    self.hideSortTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenSortView)];
    
    [self createTableView];
    [self createMoreHotPlaceView];
    [self createYueyouButton];
    [self createSortView];
    [self createLikeAlert];
}



#pragma mark - View

- (void)setUpNavigationTabBar {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.914 green:0.404 blue:0.063 alpha:1.000];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [searchButton setTitle:@"搜索目的地或人" forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor colorWithRed:0.933 green:0.592 blue:0.373 alpha:1.000] forState:UIControlStateNormal];
    searchButton.backgroundColor = [UIColor colorWithRed:0.867 green:0.349 blue:0.039 alpha:1.000];
    searchButton.frame = CGRectMake(-50, 0, 300, 30);
    searchButton.layer.cornerRadius = 15;
    searchButton.layer.masksToBounds = YES;
    [searchButton addTarget:self action:@selector(searchBarAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = searchButton;

    
    self.sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sortButton addTarget:self action:@selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
    [_sortButton setImage:[UIImage imageNamed:@"sort"] forState:UIControlStateNormal];
    [_sortButton setTitle:@" " forState:UIControlStateNormal];
    [_sortButton setFrame:CGRectMake(0, 0, 90, 30)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_sortButton];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, self.view.dk_height - 64 - 49) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = [self createTableHeaderView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTravelData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadTravelData)];
    [_tableView registerClass:[TravelGoTableCell class] forCellReuseIdentifier:kTravelGoTableCell];
}


- (UIView *)createTableHeaderView {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.dk_width, _tableView.dk_height / 5 * 2)];
    backView.backgroundColor = [UIColor whiteColor];
    // 热门目的地的 collectionView
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    fl.minimumInteritemSpacing = 0;
    fl.minimumLineSpacing = 0;
    fl.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.hotPlaceCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
    [backView addSubview:_hotPlaceCollection];
    [_hotPlaceCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(backView);
        make.height.mas_equalTo(kHotCollectionHeight);
        make.width.mas_equalTo(backView).offset(-50);
    }];
    _hotPlaceCollection.backgroundColor = [UIColor whiteColor];
    [_hotPlaceCollection registerClass:[HotPlaceCollectionCell class] forCellWithReuseIdentifier:kHotPlaceCollectionCell];
    _hotPlaceCollection.dataSource = self;
    _hotPlaceCollection.delegate = self;
    
    // 更多热门目的地Button
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moreButton.backgroundColor = [UIColor whiteColor];
    [moreButton setImage:[UIImage imageNamed:@"downArrow"] forState:UIControlStateNormal];
    [backView addSubview:moreButton];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_hotPlaceCollection.mas_right);
        make.height.mas_equalTo(_hotPlaceCollection);
        make.top.right.mas_equalTo(backView);
    }];
    [moreButton addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 轮播图
    self.bannerCycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    [backView addSubview:_bannerCycleView];
    [_bannerCycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_hotPlaceCollection.mas_bottom);
        make.left.right.bottom.mas_equalTo(backView);
    }];
    _bannerCycleView.showPageControl = NO;
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    [backView addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.mas_equalTo(backView);
    }];
    pageControl.numberOfPages = 1;
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    return backView;
}

- (void)createMoreHotPlaceView {
    self.hotPlaceLabel = [[UILabel alloc] init];
    _hotPlaceLabel.text = @"热门目的地";
    [self.view addSubview:_hotPlaceLabel];
    [_hotPlaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).offset(15);
        make.height.mas_equalTo(kHotCollectionHeight);
    }];
    _hotPlaceLabel.textAlignment = NSTextAlignmentCenter;
    
    self.moreHotPlaceBackView = [[UIView alloc] init];
    [self.view addSubview:_moreHotPlaceBackView];
    [_moreHotPlaceBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(_hotPlaceLabel.mas_bottom);
        make.bottom.mas_equalTo(self.view);
    }];
    _moreHotPlaceBackView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.95];
    
    // moreHotPlaceCollectionView
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    self.moreHotPlaceCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
    [_moreHotPlaceBackView addSubview:_moreHotPlaceCollection];
    [_moreHotPlaceCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(_moreHotPlaceBackView).offset(20);
        make.right.mas_equalTo(_moreHotPlaceBackView).offset(-20);
        make.bottom.mas_equalTo(_moreHotPlaceBackView);
    }];
    _moreHotPlaceCollection.backgroundColor = [UIColor clearColor];
    
    _moreHotPlaceCollection.dataSource = self;
    _moreHotPlaceCollection.delegate = self;
    [_moreHotPlaceCollection registerClass:[HotPlaceCollectionCell class] forCellWithReuseIdentifier:kMoreHotPlaceCollectionCell];
    
    _hotPlaceLabel.alpha = 0;
    _moreHotPlaceBackView.alpha = 0;
}


- (void)createYueyouButton {
    self.yueyouButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_yueyouButton];
    [_yueyouButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-8);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-8);
        make.height.width.mas_equalTo(kOrginateButtonWidth);
    }];
    _yueyouButton.layer.cornerRadius = 23;
    _yueyouButton.layer.masksToBounds = YES;
    [_yueyouButton setImage:[UIImage imageNamed:@"orginateYueyou"] forState:UIControlStateNormal];
    _yueyouButton.backgroundColor = [UIColor colorWithRed:0.922 green:0.455 blue:0.196 alpha:0.702];
    [_yueyouButton addTarget:self action:@selector(orginateYueyou:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createSortView {
    
    self.stopView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_stopView];
    
    self.sortView = [UIView new];
    [self.view addSubview:_sortView];
    [_sortView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@(11));
        make.right.mas_equalTo(self.view.mas_right).offset(-5);
        make.width.mas_equalTo(@111);
        make.height.mas_equalTo(@240);
    }];
    _sortView.layer.cornerRadius = 4;
    _sortView.layer.masksToBounds = YES;
    _sortView.layer.borderColor = [UIColor blackColor].CGColor;
    _sortView.layer.borderWidth = 1;
    _sortView.backgroundColor = [UIColor whiteColor];
    
    UIButton *defaultSortButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_sortView addSubview:defaultSortButton];
    [defaultSortButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_sortView.mas_left);
        make.top.mas_equalTo(_sortView.mas_top);
        make.width.mas_equalTo(_sortView.mas_width);
        make.height.mas_equalTo(_sortView.mas_height).multipliedBy(1/5.f);
    }];
    [defaultSortButton setTitle:@"默认排序" forState:UIControlStateNormal];
    [defaultSortButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIView *lineView1 = [UIView new];
    [_sortView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_sortView.mas_width).multipliedBy(2/3.f);
        make.height.mas_equalTo(@1);
        make.top.mas_equalTo(defaultSortButton.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    lineView1.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
    
    UIButton *onlyBoyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_sortView addSubview:onlyBoyButton];
    [onlyBoyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView1.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
        make.width.height.mas_equalTo(defaultSortButton);
    }];
    [onlyBoyButton setTitle:@"只看男生" forState:UIControlStateNormal];
    [onlyBoyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIView *lineView2 = [UIView new];
    [_sortView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(lineView1);
        make.top.mas_equalTo(onlyBoyButton.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    lineView2.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
    
    UIButton *onlyGirlButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_sortView addSubview:onlyGirlButton];
    [onlyGirlButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(defaultSortButton);
        make.top.mas_equalTo(lineView2.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    [onlyGirlButton setTitle:@"只看女生" forState:UIControlStateNormal];
    [onlyGirlButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIView *lineView3 = [UIView new];
    [_sortView addSubview:lineView3];
    [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(lineView1);
        make.top.mas_equalTo(onlyGirlButton.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    lineView3.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
    
    UIButton *byDistanceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_sortView addSubview:byDistanceButton];
    [byDistanceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(defaultSortButton);
        make.top.mas_equalTo(lineView3.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    [byDistanceButton setTitle:@"按距离" forState:UIControlStateNormal];
    [byDistanceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIView *lineView4 = [UIView new];
    [_sortView addSubview:lineView4];
    [lineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(lineView1);
        make.top.mas_equalTo(byDistanceButton.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    lineView4.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
    
    UIButton *byPopularityButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_sortView addSubview:byPopularityButton];
    [byPopularityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(defaultSortButton);
        make.top.mas_equalTo(lineView4.mas_bottom);
        make.centerX.mas_equalTo(defaultSortButton.mas_centerX);
    }];
    [byPopularityButton setTitle:@"按人气" forState:UIControlStateNormal];
    [byPopularityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.whiteTriangleImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.dk_width - 50, 0, 13, 13)];
    [self.view addSubview:_whiteTriangleImage];
    self.whiteTriangleImage.image = [UIImage imageNamed:@"whiteTriangle"];
    _whiteTriangleImage.backgroundColor = [UIColor clearColor];

    
    [defaultSortButton addTarget:self action:@selector(sortTravelArray:) forControlEvents:UIControlEventTouchUpInside];
    [onlyBoyButton addTarget:self action:@selector(sortTravelArray:) forControlEvents:UIControlEventTouchUpInside];
    [onlyGirlButton addTarget:self action:@selector(sortTravelArray:) forControlEvents:UIControlEventTouchUpInside];
    [byDistanceButton addTarget:self action:@selector(sortTravelArray:) forControlEvents:UIControlEventTouchUpInside];
    [byPopularityButton addTarget:self action:@selector(sortTravelArray:) forControlEvents:UIControlEventTouchUpInside];
//    _stopView.backgroundColor = [UIColor redColor];
    _stopView.hidden = YES;
    
    _whiteTriangleImage.hidden = YES;
    _sortView.hidden = YES;
}

- (void)createLikeAlert {
    self.likeAlertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, kHotCollectionHeight)];
    [self.view addSubview:_likeAlertLabel];
    _likeAlertLabel.backgroundColor = [UIColor colorWithRed:0.522 green:0.765 blue:0.894 alpha:1.000];
    _likeAlertLabel.textColor = [UIColor whiteColor];
    _likeAlertLabel.alpha = 0;
    _likeAlertLabel.textAlignment = NSTextAlignmentCenter;
}


#pragma mark - Data

- (void)refreshTravelData {
    MBProgressHUD *hud;
    if (isFistLoad) {
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = NSLocalizedString(@"加载中...", @"HUD preparing title");
        hud.minSize = CGSizeMake(150.f, 100.f);
    }
    
    NSString *filterString = @"";
    switch (_sortType) {
        case SortTypeDefault:
            filterString = @"";
            break;
        case SortTypeOnlyBoy:
            filterString = @"boy";
            break;
        case SortTypeOnlyGirl:
            filterString = @"girl";
            break;
        case SortTypeByDistance:
            filterString = @"dis";
            break;
        case SortTypeByPopularity:
            filterString = @"hot";
            break;
        default:
            break;
    }
    
    NSString *tocken = [[NSUserDefaults standardUserDefaults] objectForKey:@"tocken"];
    if (tocken.length == 0) {
        tocken = @"";
    }
    NSString *urlString = [NSString stringWithFormat:@"http://api.miaotu.net/v2/yueyou/list?token=%@&page=1&num=10&latitude=38.883453&longitude=121.544453&filter=%@", tocken, filterString];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};    
    [ZWYNetTool GET:urlString andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.travelModel = [TravelModel modelWithDic:result];
        [self setData];
        [self.tableView.mj_header endRefreshing];
        _currentPage = 1;
        [hud hideAnimated:YES];
        isFistLoad = NO;
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)setData {
    NSMutableArray *cycleImageUrlArray = [NSMutableArray array];
    for (Banner *banner in _travelModel.Banner) {
        [cycleImageUrlArray addObject:banner.PicUrl];
    }
    if (cycleImageUrlArray.count > 0) {
        [cycleImageUrlArray addObject:cycleImageUrlArray.firstObject];
    }    
    [_bannerCycleView setImageURLStringsGroup:cycleImageUrlArray];
    [_hotPlaceCollection reloadData];
    [_moreHotPlaceCollection reloadData];
    
    self.travelItemArray = [NSMutableArray array];
    self.travelItemArray = self.travelModel.Items.mutableCopy;
    self.topicArray = self.travelModel.Topic;
    
    [_tableView reloadData];
}

- (void)loadTravelData {
    _currentPage += 1;
    
    NSString *filterString = @"";
    switch (_sortType) {
        case SortTypeDefault:
            filterString = @"";
            break;
        case SortTypeOnlyBoy:
            filterString = @"boy";
            break;
        case SortTypeOnlyGirl:
            filterString = @"girl";
            break;
        case SortTypeByDistance:
            filterString = @"dis";
            break;
        case SortTypeByPopularity:
            filterString = @"hot";
            break;
        default:
            break;
    }
    NSString *tocken = [[NSUserDefaults standardUserDefaults] objectForKey:@"tocken"];
    if (tocken.length == 0) {
        tocken = @"";
    }
    NSString *urlStr = [NSString stringWithFormat:@"http://api.miaotu.net/v2/yueyou/list?token=%@&page=%zd&num=10&latitude=38.883453&longitude=121.544453&filter=%@", tocken, _currentPage, filterString];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:urlStr andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        TravelModel *travelModel = [TravelModel modelWithDic:result];
        for (TravelItem *item in travelModel.Items) {
            [self.travelItemArray addObject:item];
        }
        [_tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailureBlock:^(NSError *error) {
        
    }];
}


#pragma mark - collectionView DataSource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _hotPlaceCollection) {
        return CGSizeMake(collectionView.dk_width / 6, collectionView.dk_height);
    }
    if (collectionView == _moreHotPlaceCollection) {
        return CGSizeMake((_moreHotPlaceCollection.dk_width - 3 * 20) / 4, 35);
    }
    return CGSizeZero;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _hotPlaceCollection) {
        return 6;
    }
    if (collectionView == _moreHotPlaceCollection) {
        return _travelModel.CityList.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _hotPlaceCollection) {
        HotPlaceCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHotPlaceCollectionCell forIndexPath:indexPath];
        cell.cityLabel.text = _travelModel.CityList[indexPath.item];
        cell.cityLabel.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    if (collectionView == _moreHotPlaceCollection) {
        HotPlaceCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMoreHotPlaceCollectionCell forIndexPath:indexPath];
        cell.cityLabel.text = _travelModel.CityList[indexPath.item];
        cell.layer.cornerRadius = 17;
        cell.layer.masksToBounds = YES;
        cell.layer.borderColor = [UIColor colorWithWhite:0.706 alpha:1.000].CGColor;
        cell.layer.borderWidth = 1;
        cell.cityLabel.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    return nil;
}

#pragma mark - collectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HotPlaceCollectionCell *cell = (HotPlaceCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    SearchViewController *searchVC = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:[NSBundle mainBundle]];
    UINavigationController *searchNaVC = [[UINavigationController alloc] initWithRootViewController:searchVC];
    searchVC.cityString = cell.cityLabel.text;
    [self presentViewController:searchNaVC animated:YES completion:^{
        
    }];
}

#pragma mark - tableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _travelItemArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        // 返回那个cell
        TravelGoTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kTravelGoTableCell forIndexPath:indexPath];
        cell.topicArray = self.topicArray;
        cell.sendTidBlock = ^(NSInteger tid) {
            NSLog(@"传过来的东西: %ld", tid);
        };
        return cell;
    }
    TravelTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kTravelTableCell];
    if (cell == nil) {
        cell = [[TravelTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTravelTableCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.travelItem = _travelItemArray[indexPath.section];
    cell.delegate = self;
    
    return cell;
}

#pragma mark - tableView Delegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 350;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        return 110;
    }
    TravelTableCell *cell = (TravelTableCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGRect rect = [cell.rootLayout estimateLayoutRect:CGSizeMake(tableView.frame.size.width, 0)];
    return rect.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LineDetailViewController *lineDetailVC = [[LineDetailViewController alloc] init];
    lineDetailVC.yid = _travelItemArray[indexPath.section].Yid;
    [self.navigationController pushViewController:lineDetailVC animated:YES];
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [UIView animateWithDuration:0.2 animations:^{
        [_yueyouButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kOrginateButtonWidth);
        }];
        [_yueyouButton layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            [_yueyouButton setImage:[UIImage imageNamed:@"orginateYueyou"] forState:UIControlStateNormal];
            [_yueyouButton setTitle:nil forState:UIControlStateNormal];
        }];
    }];
    _popYueyouFlag = NO;
}

#pragma mark - Action
- (void)moreAction:(UIButton *)moreButton {
    if (flag) {
        [UIView animateWithDuration:0.5 animations:^{
            moreButton.transform = CGAffineTransformMakeRotation(M_PI);
        } completion:^(BOOL finished) {
            flag = NO;
            // 此时箭头朝上
//            NSLog(@"箭头向上, 出现那个View");
            [UIView animateWithDuration:0.2 animations:^{
                _hotPlaceCollection.alpha = 0;
                _hotPlaceLabel.alpha = 1;
                _moreHotPlaceBackView.alpha = 1;
            }];
        }];
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            moreButton.transform = CGAffineTransformMakeRotation(0);
        } completion:^(BOOL finished) {
            flag = YES;
//            NSLog(@"箭头向下, 退出那个view");
            [UIView animateWithDuration:0.2 animations:^{
                _hotPlaceCollection.alpha = 1;
                _hotPlaceLabel.alpha = 0;
                _moreHotPlaceBackView.alpha = 0;
            }];
        }];
    }
}

- (void)searchBarAction {
    SearchViewController *searchVC = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:[NSBundle mainBundle]];
    UINavigationController *searchNaVC = [[UINavigationController alloc] initWithRootViewController:searchVC];
    [self presentViewController:searchNaVC animated:YES completion:^{
        
    }];
}

- (void)sortAction {
    _whiteTriangleImage.hidden = !_whiteTriangleImage.hidden;
    _sortView.hidden = !_sortView.hidden;
    _stopView.hidden = !_stopView.hidden;
    if (_whiteTriangleImage.hidden == NO) {
        [self.view addGestureRecognizer:_hideSortTap];
    }
}

- (void)hiddenSortView {
    _whiteTriangleImage.hidden = YES;
    _sortView.hidden = YES;
    _stopView.hidden = YES;
    [self.view removeGestureRecognizer:_hideSortTap];
}

- (void)sortTravelArray:(UIButton *)button {
    NSString *titleString = button.titleLabel.text;
    if ([titleString isEqualToString:@"默认排序"]) {
        self.sortType = SortTypeDefault;
        [_sortButton setTitle:@" " forState:UIControlStateNormal];
    } else if ([titleString isEqualToString:@"只看男生"]) {
        self.sortType = SortTypeOnlyBoy;
        [_sortButton setTitle:@"男生" forState:UIControlStateNormal];
    } else if ([titleString isEqualToString:@"只看女生"]) {
        self.sortType = SortTypeOnlyGirl;
        [_sortButton setTitle:@"女生" forState:UIControlStateNormal];
    } else if ([titleString isEqualToString:@"按距离"]) {
        self.sortType = SortTypeByDistance;
        [_sortButton setTitle:@"按距离" forState:UIControlStateNormal];
    } else if ([titleString isEqualToString:@"按人气"]) {
        self.sortType = SortTypeByPopularity;
        [_sortButton setTitle:@"按人气" forState:UIControlStateNormal];
    } else {
        NSLog(@"不知道按什么排序");
    }
    _whiteTriangleImage.hidden = YES;
    _sortView.hidden = YES;
    _stopView.hidden = YES;
    [self.tableView.mj_header beginRefreshing];
}

- (void)orginateYueyou:(UIButton *)button {
    if (_popYueyouFlag == YES) {
        NSLog(@"发起约游");
        
        YueyouViewController *yueyouVC = [[YueyouViewController alloc] init];
        [self.navigationController pushViewController:yueyouVC animated:YES];
        
        [UIView animateWithDuration:0.2 animations:^{
            [_yueyouButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(kOrginateButtonWidth);
            }];
            [_yueyouButton layoutIfNeeded];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                [_yueyouButton setImage:[UIImage imageNamed:@"orginateYueyou"] forState:UIControlStateNormal];
                [_yueyouButton setTitle:nil forState:UIControlStateNormal];
            }];
        }];
        _popYueyouFlag = NO;
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            [_yueyouButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(kOrginateButtonWidth * 2);
            }];
            [_yueyouButton layoutIfNeeded];
        } completion:^(BOOL finished) {
            [_yueyouButton setImage:nil forState:UIControlStateNormal];
            [_yueyouButton setTitle:@"发起约游" forState:UIControlStateNormal];
            _popYueyouFlag = YES;
        }];
    }
}

- (void)sendUid:(NSString *)uid {
    PersonalHomeViewController *perHomeVC = [[PersonalHomeViewController alloc] init];
    perHomeVC.Uid = uid;
    [self.navigationController pushViewController:perHomeVC animated:YES];
}

- (void)sendPicArrayAndImageUrl:(NSDictionary *)dic {
    
    PhotoAlbumViewController *picVC = [[PhotoAlbumViewController alloc] init];
    picVC.picDic = dic;
    [self.navigationController pushViewController:picVC animated:YES];
}

- (void)sendLRJMsgDic:(NSDictionary *)msgDic {
    NSString *msg = msgDic[@"Msg"];
    if ([msg isEqualToString:@"not Login"]) {
        LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
        UINavigationController *loginNaVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNaVC animated:YES completion:nil];
    } else if ([msg isEqualToString:@"取消喜欢"] || [msg isEqualToString:@"喜欢成功"]) {
        _likeAlertLabel.text = msg;
        [UIView animateWithDuration:2 animations:^{
            _likeAlertLabel.alpha = 0.8;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:2 animations:^{
                _likeAlertLabel.alpha = 0;
            } completion:^(BOOL finished) {
                
            }];
        }];
    } else if ([msg isEqualToString:@"reply"]) {
        ReplyViewController *replyVC = [[ReplyViewController alloc] init];
        replyVC.yid = msgDic[@"yid"];
        [self.navigationController pushViewController:replyVC animated:YES];
    } else if ([msg isEqualToString:@"join"]) {
        NSLog(@"join");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
